package com.bizfx.component.fields
{
	import mx.containers.FormItem;
	import mx.events.ChildExistenceChangedEvent;
	
	public class FormItemFieldHolder extends FormItem implements IFieldHolder
	{
		private var _field:IField;

		public function get field():IField
		{
			return _field;
		}

		public function set field(value:IField):void
		{
			_field = value;
		}

		public function get fieldTitle():String
		{
			return this.label;
		}
		
		public function set fieldTitle(value:String):void
		{
			this.label = value;
		}
		
		public function get fieldRequired():Boolean
		{
			return this.required;
		}
		
		public function set fieldRequired(value:Boolean):void
		{
			this.required = value;
		}
		
		public function FormItemFieldHolder()
		{
			super();
			this.addEventListener(ChildExistenceChangedEvent.CHILD_ADD, childAddHandler);
		}
		
		private function childAddHandler(event:ChildExistenceChangedEvent):void {
			if (this._field == null && event.relatedObject is IField) {
				this._field = IField(event.relatedObject);
				if (event.relatedObject is IValidationField) {
					this.required = IValidationField(this._field).required;
				}
			}
		}
	}
}